<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Page_Controllers_Page extends Gio_Core_Controller
{
	/* ========== Frontend actions ========================================== */
	
	/**
	 * View page details
	 * 
	 * @return void
	 */
	public function detailsAction()
	{
		$page    = Modules_Page_Services_Page::factory();
		if (null == $page) {
			Modules_Core_Services_Exception::error('DATA_NOT_FOUND');
		}
		
		/**
		 * Add meta description tag
		 */
//		$description = strip_tags($page->description);
//		$this->view->headMeta()->setName('description', $description);
		
		$this->view->page = $page;
	}
	
	/* ========== Backend actions =========================================== */
	
	public function activateAction()
	{
		$this->setNoRender();
		$this->disableLayout();
		
		$request = $this->getRequest();
		if (!$request->isPost()) {
			return;
		}
		$response = 'RESULT_NOT_OK';
		$pageId = $request->getPost('page_id');
		$page = Modules_Page_Services_Page::getById($pageId);
		
		if (null == $page){
			$this->getResponse()->setBody($response);
			return;	
		}
		
		$act = $request->getPost('act');
		switch ($act) {
			case 'activate':
				$status = 'active';
				break;
			case 'deactivate':
			default:
				$status = 'inactive';
				break;
		}
		$page['status'] = $status;
		$response = 'RESULT_OK';
		Modules_Page_Services_Page::activate($page);
		$this->getResponse()->setBody($response);
	}
	
	/**
	 * Add new page
	 * 
	 * @return void
	 */
	public function addAction()
	{
		$request = $this->getRequest();
		
		$sourceId   = $request->getParam('source_id');
		$sourcePage = (null == $sourceId) ? null : Modules_Page_Services_Page::getById($sourceId);
		
		$configs = Gio_Core_Config_Xml::getConfig('web');
		$this->view->frontendTemplate = $configs->template;
		$this->view->frontendSkin 	  = $configs->skin;
		
		$this->view->sourcePage 	  = $sourcePage;
		$this->view->lang 			  = $request->getParam('lang');
		
		if ($request->isPost()) {
			$user = Gio_Core_Session::getUser();
			
			$pageData = $request->getPost('page');
			$pageData = Modules_Page_Services_Page::validate($pageData);
			
			if (isset($pageData['messages_error']) && $pageData['messages_error']) {
				$this->view->errorMessages = $pageData['messages'];
				$this->view->pageData = $pageData;
				return;
			}
			
			/**
			 * Check page slug exist 
			 */
			if (Modules_Page_Services_Page::getBySlug($pageData['slug'])) {
				$pageData['messages']['slug'] = 'page_slug_exist';
				$this->view->errorMessages = $pageData['messages'];
				$this->view->pageData = $pageData;
				return;
			}
			
			$page = array(
				'name' 			=> $this->view->STRING->escape($pageData['name']),
				'slug' 			=> $this->view->STRING->escape($pageData['slug']),
				'description'	=> $pageData['description'],
				'content'		=> $pageData['content'],
				'parent_id' 	=> $pageData['parent_id'],
				'num_views'     => 0,
				'created_date' 	=> date('Y-m-d H:i:s'),
				'modified_date' => date('Y-m-d H:i:s'),
				'user_id' 		=> $user['user_id'],
				'language'	    => $pageData['language'],
				'status'		=> $pageData['status'],
			);
			
			$sourceItem = isset($pageData['source_item']) 
								? $pageData['source_item'] : null;
			Modules_Page_Services_Page::add($page, $sourceItem);
			
			Gio_Core_Messenger::getInstance()
								->addMessage($this->view->TRANSLATOR->translator('page_actions_add_success'));
			$this->redirect($this->view->url('page_page_add'));
		}
	}
	
	/**
	 * Delete page
	 * 
	 * @return void
	 */
	public function deleteAction()
	{
		$this->setNoRender();
		$this->disableLayout();
		
		$request = $this->getRequest();
		if (!$request->isPost()) {
			return;
		}
		$pageId = $request->getPost('page_id');
		$page = Modules_Page_Services_Page::getById($pageId);
		
		$condition = array('parent_id' => $pageId);
		if (null == $page || Modules_Page_Services_Page::count($condition) > 0){
			$this->getResponse()->setBody('RESULT_NOT_OK');
			return;	
		}
		
		/**
		 * Delete category
		 */
		Modules_Page_Services_Page::delete($page);
		$this->getResponse()->setBody('RESULT_OK');
	}
	
	/**
	 * Edit page
	 * 
	 * @return void
	 */
	public function editAction()
	{
		$request = $this->getRequest();
		$pageId  = $request->getParam('page_id');
		$page    = Modules_Page_Services_Page::getById($pageId);
		
		if (null == $page) {
			throw new Exception('Not found page with id of ' . $pageId);
		}
		
		$configs = Gio_Core_Config_Xml::getConfig('web');
		$this->view->frontendTemplate = $configs->template;
		$this->view->frontendSkin 	  = $configs->skin;
		
		$sourcePage = Modules_Page_Services_Page::getSource($page);
		$request->setParam('lang', $page['language']);
		$this->view->lang 			  = $page['language'];
		
		$this->view->pageData	= $page;
		$oldParentId = $page['parent_id'];
		$this->view->sourcePage = $sourcePage;
		
		if ($request->isPost()) {
			$pageData = $request->getPost('page');
			$pageData = Modules_Page_Services_Page::validate($pageData);
			
			if (isset($pageData['messages_error']) && $pageData['messages_error']) {
				$this->view->errorMessages = $pageData['messages'];
				$pageData['page_id'] = $page['page_id'];
				$this->view->pageData = $pageData;
				return;
			}
			
			/**
			 * Check page slug exist 
			 */
			if (Modules_Page_Services_Page::getBySlug($pageData['slug'], $page['page_id'])) {
				$pageData['messages']['slug'] = 'page_slug_exist';
				$this->view->errorMessages = $pageData['messages'];
				$pageData['page_id'] = $page['page_id'];
				$this->view->pageData = $pageData;
				return;
			}
			
			$page = $page;
			$page['name'] 			= $this->view->STRING->escape($pageData['name']);
			$page['slug'] 			= $this->view->STRING->escape($pageData['slug']);
			$page['description'] 	= $pageData['description'];
			$page['content'] 		= $pageData['content'];
			$page['parent_id'] 		= $pageData['parent_id'];
			$page['modified_date']  = date('Y-m-d H:i:s');
			$page['status'] 		= (null == $pageData['status']) ? 'inactive' : $pageData['status'];
			 
			$sourceItem = isset($pageData['source_item']) 
								? $pageData['source_item'] : null;
			Modules_Page_Services_Page::update($page, $oldParentId, $sourceItem);
			
			Gio_Core_Messenger::getInstance()
								->addMessage($this->view->TRANSLATOR->translator('page_actions_edit_success'));
			$this->redirect($this->view->url('page_page_edit', $page));
		}
	}
	
	/**
	 * List pages
	 * 
	 * @return void
	 */
	public function listAction()
	{
		$request = $this->getRequest();
		
		$lang    = $request->getParam('lang', Gio_Core_Config_Xml::getConfig('web')->language);
		$pages   = Modules_Page_Services_Page::getTree($lang);
		$this->view->pages = $pages;
		
		$this->view->pageDao = new Modules_Page_Models_Mysql_Page();
	}
	
	/**
	 * Update pages's order
	 * 
	 * @return void
	 */
	public function orderAction()
	{
		$this->setNoRender();
		$this->disableLayout();
		
		$request = $this->getRequest();
		if (!$request->isPost()) {
			$this->getResponse()->setBody('RESULT_NOT_OK');
			return;
		}
		$response = 'RESULT_NOT_OK';
		$data = $request->getPost('data');
		$json = new Services_JSON();
		$data = $json->decode($data);
		
		foreach ($data as $page) {
			Modules_Page_Services_Page::updateOrder(array(
				'page_id' 	  => $page->id,
				'parent_id'   => $page->parent_id,
				'left_id'     => $page->left_id,
				'right_id'    => $page->right_id,
			));
		}
			
		$this->getResponse()->setBody('RESULT_OK');
	}
}
